#!/bin/bash
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

#
# Copyright 2019 Joyent, Inc.
#

#
# Quick hack script that looks in /Joyent_Dev/public/builds/...
# for SDC components for which there is no "release-YYYYMMDD"
# build. These are ones that you may need to manually kick
# off in jenkins.
#

if [[ -n "$TRACE" ]]; then
    export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
    set -o xtrace
fi

set -o errexit
set -o pipefail

#---- globals

TOP=$(cd $(dirname $0)/../; pwd)

#---- support stuff

function fatal {
    echo "$(basename $0): error: $1"
    exit 1
}

function usage() {
    if [[ -n "$1" ]]; then
        echo "error: $1"
        echo ""
    fi
    echo "usage:"
    echo "  ls-missing-release-builds [-s] RELEASE [TARGETS...]"
    echo "   -s     Do a smartos-only build"
    echo "          (this influences how we launch the platform build)"
    echo ""
    echo "example:"
    echo "  ls-missing-release-builds release-20140703"
    exit 1
}

# Specify the type of platform build to do.
PLATFORM_BUILD_FLAVOR='triton-and-smartos'

#---- mainline

while getopts 'sh' opt; do
    case $opt in
    s) PLATFORM_BUILD_FLAVOR=smartos;;
    h) usage;;
    :) usage "-$OPTARG requires an argument";;
    \?) usage "illegal option: -$OPTARG";;
    esac
    shift
done

release=$1
if [[ -z "$release" ]]; then
    usage "missing 'RELEASE' argument given"
fi
if [[ -z "$(echo $release | grep '^release-[0-9]\{8\}$' || true)" ]]; then
    usage "given RELEASE, '$release', doesn't match /release-\d{8}/"
fi
shift

for command in jr json; do
    $command -h 2>&1 > /dev/null || fatal "Unable to run $command, please check your \$PATH"
done

if [[ -z "$JR_MANIFESTS" ]]; then
    fatal "$(basename $0): error: \$JR_MANIFESTS should be set in the environment. \
    Example value: \
    JR_MANIFESTS=/Volumes/projects/triton.git/tools/jr-manifest.json,/Volumes/projects/manta.git/tools/jr-manifest.json,/Volumes/projects/smartos-live.git/tools/jr-manifest.json"
fi

filtered_targets="$*"
if [[ -z "$targets" ]]; then
    # we get the mg label since that's part of the component manta path
    # we get the buildisprivate label to find non-public builds
    # we get name since that's a component of the jenkins job to launch
    targets=$(jr list -H -l mg,release -o labels.mg,labels.buildisprivate,name)
fi

# Ensure mls is setup properly at all.
mls /Joyent_Dev/public/builds >/dev/null \
    || fatal "cannot list /Joyent_Dev/public/builds"

echo "$targets" | while read target buildisprivate gitrepo; do

    # if we've been asked to only look for specific targets, then
    # we should skip to the next item if this target isn't in our list
    if [[ -n "$filtered_targets" ]]; then
        process_target=""
        for filter in $filtered_targets; do
            if [[ "$target" == "$filter" ]]; then
                process_target=true
            fi
        done
        if [[ -z "$process_target" ]]; then
            continue
        fi
    fi

    if [[ "$buildisprivate" == "true" ]]; then
        latest_mpath=/Joyent_Dev/stor/builds/$target/$release-latest
    else
        latest_mpath=/Joyent_Dev/public/builds/$target/$release-latest
    fi

    case $target in
        "platform")
            # For the platform build, only the 'smartos-live' repository
            # is relevant, ignore the others.
            if [[ "$gitrepo" != "smartos-live" ]]; then
                continue
            fi
            PLAT_FLAVOR_ARG="-F $PLATFORM_BUILD_FLAVOR"
            ;;
        *)
            PLAT_FLAVOR_ARG=""
            ;;
    esac

    latest=$(mls $latest_mpath 2>/dev/null || true)
    if [[ -z "$latest" ]]; then
        echo "# $target, '$latest_mpath' does not exist" >&2
        echo "./tools/releng/launch-build ${PLAT_FLAVOR_ARG} -b $release -g $gitrepo $target"
    fi
done
